//
//  HEGoodDetailsTitleCell.m
//  HealthElderly
//
//  Created by 吴欧 on 2020/10/20.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEGoodDetailsTitleCell.h"

@interface HEGoodDetailsTitleCell ()

@property (nonatomic, strong) UILabel *priceLb;
@property (nonatomic, strong) UILabel *oldPriceLb;
@property (nonatomic, strong) UILabel *salesLb;
@property (nonatomic, strong) UILabel *titleLb;
@property (nonatomic, assign) BOOL hasCreateTags;
@property MASConstraint * bottomConstraint;

@property (nonatomic, strong) UILabel *subTitleLb;
@property (nonatomic, strong) UILabel *noteLb;

@end

@implementation HEGoodDetailsTitleCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    // 价格
    _priceLb = [UILabel new];
    _priceLb.textColor = KHexColor(@"#FF5757");
    _priceLb.font = DYBlodFont(18);
    _priceLb.numberOfLines = 0;
    [self.contentView addSubview:_priceLb];

    // 原始价格
    _oldPriceLb = [UILabel new];
    _oldPriceLb.textColor = KHexColor(@"#AAAAAA");
    _oldPriceLb.font = DYFont(14);
    [self.contentView addSubview:_oldPriceLb];
    
    // 销量
    _salesLb = [UILabel new];
    _salesLb.textColor = KHexColor(@"#AAAAAA");
    _salesLb.font = DYFont(14);
    [self.contentView addSubview:_salesLb];
    
    // 标题
    _titleLb = [UILabel new];
    _titleLb.textColor = KHexColor(@"#3A3A3A");
    _titleLb.font = DYBlodFont(18);
    [self.contentView addSubview:_titleLb];
    
    // 描述
    _subTitleLb = [UILabel new];
    _subTitleLb.textColor = CNavLightBlackColor;
    _subTitleLb.font = DYFont(16);
    _subTitleLb.numberOfLines = 2;
    [self.contentView addSubview:_subTitleLb];
    
    // 备注
    _noteLb = [UILabel new];
    _noteLb.textColor = CNavDargGrayColor;
    _noteLb.font = DYFont(16);
    _noteLb.numberOfLines = 0;
    [self.contentView addSubview:_noteLb];
    
    [_priceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(19);
        make.left.equalTo(self.contentView).mas_offset(15);
    }];
    
    [_priceLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    [_salesLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_priceLb);
        make.right.equalTo(self.contentView).mas_offset(-15);
    }];

    [_oldPriceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_priceLb.mas_right).mas_offset(10);
        make.right.equalTo(self.contentView).mas_offset(-15);
        make.bottom.equalTo(_priceLb);
    }];
    
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_priceLb.mas_bottom).mas_offset(10);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
    }];

    [_subTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLb.mas_bottom).mas_offset(10);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
    }];
    [_noteLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_subTitleLb.mas_bottom).mas_offset(10);
        make.left.equalTo(self.contentView).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset(-15);
        self.bottomConstraint = make.bottom.equalTo(self.contentView).mas_offset(-10);
    }];
}
 
- (void)setModel:(HEGoodsModel *)model {
    _model = model;
    
    _priceLb.text = [NSString stringWithFormat:@"￥%@", model.Kprice];
    _oldPriceLb.attributedText = model.KOrangeprice;
    _salesLb.text = model.kScaleStr;
    _titleLb.text = model.name;
    _subTitleLb.text = model.subTitle;
    _noteLb.text = model.note;
    
    [self initTagView];
}

- (void)initTagView {
    
  
    if(_model == nil) {
   
        return;
    }
    
    NSArray *checks = @[];
    checks = _model.tagList == nil ? @[] : _model.tagList;
    
    if (_hasCreateTags) {
        return;
    }
    [_noteLb mas_updateConstraints:^(MASConstraintMaker *make) {
        if (checks.count == 0) {
            _bottomConstraint = make.bottom.equalTo(self.contentView).mas_offset(-10);
        }else {
            [_bottomConstraint uninstall];
            _bottomConstraint = nil;
        }
    }];
     
    NSMutableArray *btnArr = [NSMutableArray array];
    
    //存放上一个button
    UIButton *buttonBefore;
    //存放button距离左侧的宽度，来确定是否需要换行
    __block CGFloat right = 0.0;
    //间距
    CGFloat margin = 15.f;
    for(int i = 0; i < checks.count; i++) {
        HEGoodsTagModel *tag = checks[i];
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        backBtn.titleLabel.font = DYFont(14);
        backBtn.adjustsImageWhenHighlighted = NO;
        [self.contentView addSubview:backBtn];
        [backBtn setTitle:tag.name forState:UIControlStateNormal];
        [backBtn setImage:IMAGE_NAMED(@"check_small") forState:UIControlStateNormal];
        [backBtn setTitleColor:KHexColor(@"#AAAAAA") forState:UIControlStateNormal];
        [backBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -7)];
        [btnArr addObject:backBtn];
    
        CGFloat fitWidth = [tag.name widthForFont:backBtn.titleLabel.font] + 15;

        [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(fitWidth);
            make.height.mas_equalTo(20);
            if(i==0){
              make.left.equalTo(self.contentView).offset(margin);
              make.top.equalTo(_noteLb.mas_bottom).offset(12);
              right = margin + fitWidth + margin;
            }else{
              if(right + fitWidth > KScreenWidth){
                  make.top.mas_equalTo(buttonBefore.mas_bottom).offset(margin);
                  make.left.equalTo(self.contentView).offset(margin);
                  right =  margin+fitWidth+margin;
              }else{
                  make.left.mas_equalTo(buttonBefore.mas_right).offset(margin);
                  make.top.equalTo(buttonBefore);
                  right += fitWidth+margin;
              }
            }
            
            if (i == (checks.count -1)) {
                make.bottom.equalTo(self.contentView).mas_offset(-10);
            }
        }];
        buttonBefore = backBtn;
    }
    _hasCreateTags = true;

}

- (void)refreshContent:(GDFiterCellModel *)fiterModel formModel:(GDFormModel *)formModel indexPath:(NSIndexPath *)indexPath{
    self.model = formModel.goodsModel;
}

@end
